/*     */ package com.zimbra.cs.account;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import java.security.SecureRandom;
/*     */ import java.util.HashMap;
/*     */ import org.apache.commons.codec.DecoderException;
/*     */ import org.apache.commons.codec.binary.Hex;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CsrfTokenKey
/*     */ {
/*     */   public static final int KEY_SIZE_BYTES = 32;
/*     */   private byte[] csrfTokenKey;
/*     */   private long keyVersion;
/*     */   private long keyCreatedAt;
/*  41 */   private static HashMap<String, CsrfTokenKey> tokenCache = new HashMap();
/*     */   private static CsrfTokenKey latestCsrfKey;
/*     */   
/*     */   public byte[] getKey() {
/*  45 */     return this.csrfTokenKey;
/*     */   }
/*     */   
/*     */   public long getVersion() {
/*  49 */     return this.keyVersion;
/*     */   }
/*     */   
/*     */   public long getCreated() {
/*  53 */     return this.keyCreatedAt;
/*     */   }
/*     */   
/*     */   void setKey(byte[] key) {
/*  57 */     this.csrfTokenKey = key;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   CsrfTokenKey(long version, byte[] key)
/*     */     throws ServiceException
/*     */   {
/*  66 */     this.keyVersion = version;
/*  67 */     this.keyCreatedAt = System.currentTimeMillis();
/*  68 */     if (key != null) {
/*  69 */       this.csrfTokenKey = key;
/*     */     } else {
/*  71 */       SecureRandom random = new SecureRandom();
/*  72 */       this.csrfTokenKey = new byte[32];
/*  73 */       random.nextBytes(this.csrfTokenKey);
/*     */     }
/*     */   }
/*     */   
/*     */   private CsrfTokenKey(String k) throws ServiceException {
/*  78 */     String[] parts = k.split(":");
/*  79 */     if (parts.length != 3) {
/*  80 */       throw ServiceException.INVALID_REQUEST("invalid auth token key", null);
/*     */     }
/*  82 */     String ver = parts[0];
/*  83 */     String created = parts[1];
/*  84 */     String data = parts[2];
/*     */     try
/*     */     {
/*  87 */       this.keyVersion = Long.parseLong(ver);
/*     */     } catch (NumberFormatException e) {
/*  89 */       throw ServiceException.INVALID_REQUEST("invalid auth token key version", e);
/*     */     }
/*     */     
/*     */     try
/*     */     {
/*  94 */       this.keyCreatedAt = Long.parseLong(created);
/*     */     } catch (NumberFormatException e) {
/*  96 */       throw ServiceException.INVALID_REQUEST("invalid auth token key created data", e);
/*     */     }
/*     */     
/*     */     try
/*     */     {
/* 101 */       this.csrfTokenKey = Hex.decodeHex(data.toCharArray());
/*     */     } catch (DecoderException e) {
/* 103 */       throw ServiceException.INVALID_REQUEST("invalid auth token key data", e);
/*     */     }
/*     */   }
/*     */   
/*     */   private static synchronized void refresh(boolean reload)
/*     */     throws ServiceException
/*     */   {
/* 110 */     Provisioning prov = Provisioning.getInstance();
/* 111 */     Config config = prov.getConfig();
/*     */     
/* 113 */     if (reload) {
/* 114 */       prov.reload(config);
/*     */     }
/* 116 */     String key = config.getAttr("zimbraCsrfTokenKey");
/*     */     
/* 118 */     if (key == null) {
/* 119 */       prov.reload(config);
/* 120 */       key = config.getAttr("zimbraCsrfTokenKey");
/*     */     }
/*     */     
/*     */ 
/* 124 */     if (key == null) {
/* 125 */       CsrfTokenKey csrfKey = new CsrfTokenKey(0L, null);
/* 126 */       HashMap<String, String> attrs = new HashMap();
/* 127 */       attrs.put("zimbraCsrfTokenKey", csrfKey.getEncoded());
/* 128 */       Provisioning.getInstance().modifyAttrs(config, attrs);
/* 129 */       key = config.getAttr("zimbraCsrfTokenKey");
/*     */     }
/*     */     
/* 132 */     CsrfTokenKey csrfKey = (CsrfTokenKey)tokenCache.get(key);
/* 133 */     if (csrfKey == null) {
/* 134 */       csrfKey = new CsrfTokenKey(key);
/* 135 */       tokenCache.put(key, csrfKey);
/* 136 */       tokenCache.put(Long.toString(csrfKey.keyVersion), csrfKey);
/* 137 */       if ((latestCsrfKey == null) || (latestCsrfKey.keyVersion < csrfKey.keyVersion)) {
/* 138 */         latestCsrfKey = csrfKey;
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public static synchronized CsrfTokenKey getCurrentKey() throws ServiceException {
/* 144 */     if (latestCsrfKey == null) {
/* 145 */       refresh(false);
/*     */     }
/* 147 */     return latestCsrfKey;
/*     */   }
/*     */   
/*     */   public String getEncoded() {
/* 151 */     return this.keyVersion + ":" + this.keyCreatedAt + ":" + new String(Hex.encodeHex(this.csrfTokenKey));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static CsrfTokenKey getVersion(String version)
/*     */     throws ServiceException
/*     */   {
/* 164 */     CsrfTokenKey key = (CsrfTokenKey)tokenCache.get(version);
/*     */     
/*     */ 
/*     */ 
/* 168 */     if (key == null) {
/* 169 */       refresh(false);
/*     */     }
/* 171 */     key = (CsrfTokenKey)tokenCache.get(version);
/*     */     
/*     */ 
/* 174 */     if (key == null)
/* 175 */       refresh(true);
/* 176 */     key = (CsrfTokenKey)tokenCache.get(version);
/*     */     
/*     */ 
/* 179 */     return key;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/CsrfTokenKey.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */